package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.SensitiveDto;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author wsj
 * @since 2022-03-11
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {

    @Override
    public ResponseResult listByPage(SensitiveDto dto) {
        //构建分页条件
        IPage<WmSensitive> page=new Page<>(dto.getPage(),dto.getSize());
        //构建查询条件
        LambdaQueryWrapper<WmSensitive> query=new LambdaQueryWrapper<>();
        query.like(!StringUtils.isEmpty(dto.getName()), WmSensitive::getSensitives, dto.getName());
        query.orderByDesc(WmSensitive::getCreatedTime);
        IPage<WmSensitive> iPage = page(page, query);
        return new PageResponseResult(dto.getPage(), dto.getSize(), iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public ResponseResult add(WmSensitive wmSensitive) {
        //判空
        if (wmSensitive==null|| StringUtils.isEmpty(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //不能重复
        LambdaQueryWrapper<WmSensitive> query=new LambdaQueryWrapper<>();
        query.select(WmSensitive::getSensitives);
        List<String> names = listObjs(query, o -> o.toString());
        if (names.contains(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SENSITIVES_IS_EXIST);
        }
        //保存
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateSensitve(WmSensitive wmSensitive) {
        //判空
        if (wmSensitive==null|| StringUtils.isEmpty(wmSensitive.getSensitives())||wmSensitive.getId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //不能重复
        LambdaQueryWrapper<WmSensitive> query=new LambdaQueryWrapper<>();
        query.select(WmSensitive::getSensitives);
        query.ne(WmSensitive::getId, wmSensitive.getId());
        List<String> names = listObjs(query, o -> o.toString());
        if (names.contains(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SENSITIVES_IS_EXIST);
        }
        //修改
        updateById(wmSensitive);
        return ResponseResult.okResult();
    }
}
